package com.stylefeng.guns.modular.flagglory.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.Season;
import com.stylefeng.guns.modular.flagglory.service.ISeasonService;

import javax.validation.Valid;
import java.util.List;

/**
 * 赛季控制器
 *
 * @author fengshuonan
 * @Date 2018-01-31 14:02:36
 */
@Controller
@RequestMapping("/season")
public class SeasonController extends BaseController {

    private String PREFIX = "/flagGlory/season/";

    @Autowired
    private ISeasonService seasonService;

    /**
     * 跳转到赛季首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "season.html";
    }

    /**
     * 跳转到添加赛季
     */
    @RequestMapping("/season_add")
    public String seasonAdd() {
        return PREFIX + "season_add.html";
    }

    /**
     * 跳转到修改赛季
     */
    @RequestMapping("/season_update/{seasonId}")
    public String seasonUpdate(@PathVariable Integer seasonId, Model model) {
        Season season = seasonService.selectById(seasonId);
        model.addAttribute("item", season);
        LogObjectHolder.me().set(season);
        return PREFIX + "season_edit.html";
    }

    /**
     * 获取赛季列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Season> seasons = seasonService.selectList(null);
        return seasons;
    }

    /**
     * 新增赛季
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@Valid Season season, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        seasonService.insert(season);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除赛季
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer seasonId) {
        seasonService.deleteById(seasonId);
        return SUCCESS_TIP;
    }

    /**
     * 修改赛季
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid Season season, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        seasonService.updateById(season);
        return super.SUCCESS_TIP;
    }

    /**
     * 赛季详情
     */
    @RequestMapping(value = "/detail/{seasonId}")
    @ResponseBody
    public Object detail(@PathVariable("seasonId") Integer seasonId) {
        return seasonService.selectById(seasonId);
    }

    @RequestMapping(value = "/setDefault")
    @ResponseBody
    public Object setDefault(Integer id,boolean isDefault) throws Exception{
        seasonService.setDefault(id,isDefault);
        return SUCCESS_TIP;
    }
}
